# -*- coding:utf-8 -*-
import time
from datetime import datetime
from PyQt5 import QtTest


class Timer(object):

    def __init__(self, buy_time, sleep_interval=0.5):
        self.timearray = time.strptime(buy_time, "%Y-%m-%d %H:%M:%S.%f")
        self.ts = int(time.mktime(self.timearray)) * 1000 - 3000
        self.ts2 = float(self.ts / 1000)
        self.ta = time.localtime(self.ts2)
        self.buy_time = datetime.strptime(buy_time, "%Y-%m-%d %H:%M:%S.%f")
        self.final = time.strftime("%Y-%m-%d %H:%M:%S", self.ta) + '.000'
        self.pre_time = datetime.strptime(self.final, "%Y-%m-%d %H:%M:%S.%f")
        self.sleep_interval = sleep_interval

    def start(self):
        now_time = datetime.now
        while True:
            if now_time() >= self.buy_time:
                break
            else:
                QtTest.QTest.qWait(10)

    def pre_start(self):
        now_time = datetime.now
        while True:
            if now_time() >= self.pre_time:
                break
            else:
                QtTest.QTest.qWait(10)